package com.ericsson.mna.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.StringTokenizer;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ericsson.mna.model.bs.TaskServices;
import com.ericsson.mna.model.bs.UserRegisterServices;
import com.ericsson.mna.model.pojo.DBConf;

// TODO: Auto-generated Javadoc
/**
 * Servlet implementation class AjaxController
 * eshyjen
 */
@WebServlet("/AjaxController")
public class AjaxController extends HttpServlet {

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 1L;


    /**
     * Instantiates a new ajax controller.
     *
     * @see HttpServlet#HttpServlet()
     */
    public AjaxController() {
        super();
        // TODO Auto-generated constructor stub
    }


	/**
	 * Do get.
	 *
	 * @param request the request
	 * @param response the response
	 * @throws ServletException the servlet exception
	 * @throws IOException Signals that an I/O exception has occurred.
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub

		String domainName = request.getParameter("domainName");
		String category = request.getParameter("category");
		String categoryName = request.getParameter("categoryName");
		String nm = request.getParameter("q");

		System.out.println("AjaxController doGet method");

		//Fetch sub-domain from DB
		if(domainName!=null && !domainName.equals("")){
				System.out.println("inside doGet method to tetch sub-domain");
				Properties prop=new Properties();
				prop.load(getServletContext().getResourceAsStream("/view/resources/prop/DbConf.properties"));
				DBConf dbconf=new DBConf();
				dbconf.setDriverName(prop.getProperty("driverName"));
				dbconf.setPassword(prop.getProperty("password"));
				dbconf.setUrl(prop.getProperty("url"));
				dbconf.setUserName(prop.getProperty("userName"));
				System.out.println("Testing Properties file url value:::"+dbconf.getUrl());
				UserRegisterServices urs=new UserRegisterServices(dbconf);

				response.setContentType("text/html");
		        PrintWriter out=response.getWriter();
		        System.out.println("Outside The While Loop");
		        //System.out.println(req.getParameter("region"));

		        System.out.println("domainName :"+domainName);
		        ResultSet rs = null;
		        try
		        {
		            rs=urs.getSubDomainName(domainName);

		            	while(rs.next())
		                {
		                    System.out.print("Within While Loop");
		                    out.write("<option value=\""+rs.getString(1)+"\">");
		                    out.write(rs.getString(1)+"</option>");
		                }

		        }
		        catch (Exception e)
		        {
		            System.out.println("Exception From Servlet"+e);
		            out.print("<option></option>");
		        }
		        finally {
					try {
						rs.close();
					} catch (SQLException sqe) {
						System.out.println("Exception caught in getSubDomainName :"+sqe);
					}
				}
		        return;
		}

		//Fetch Category from DB
		if(category!=null && !category.equals("")){
			System.out.println("inside doGet method to fetch category from DB");
			Properties prop=new Properties();
			prop.load(getServletContext().getResourceAsStream("/view/resources/prop/DbConf.properties"));
			DBConf dbconf=new DBConf();
			dbconf.setDriverName(prop.getProperty("driverName"));
			dbconf.setPassword(prop.getProperty("password"));
			dbconf.setUrl(prop.getProperty("url"));
			dbconf.setUserName(prop.getProperty("userName"));
			System.out.println("Testing Properties file url value:::"+dbconf.getUrl());
			TaskServices taskServ=new TaskServices(dbconf);

			response.setContentType("text/html");
	        PrintWriter out=response.getWriter();
	        System.out.println("Outside The While Loop");
	        //System.out.println(req.getParameter("region"));

	        System.out.println("domainName :"+domainName);
	        ResultSet rs = null;
	        try
	        {
	            rs=taskServ.getCategory();

	        	   out.write("<option>");
                   out.write("--Select--"+"</option>");
	           while(rs.next())
	                {
	                    //System.out.print("Within While Loop");
	                    //System.out.print("<option value=\""+rs.getString(1)+"\">");
	                    out.write("<option value=\""+rs.getString(1)+"\">");
	                    out.write(rs.getString(1)+"</option>");
	                }
	            }


	        catch (Exception e)
	        {
	            System.out.println("Exception From Servlet"+e);
	            out.print("<option></option>");
	        }
	        finally {
				try {
					rs.close();
				} catch (SQLException sqe) {
					System.out.println("Exception caught in getSubDomainName :"+sqe);
				}
			}
	        return;
	}

			//Fetch Sub-Category from DB
			if(categoryName!=null && !categoryName.equals("")){
				System.out.println("Fetch subCategory from DB: "+categoryName);
				Properties prop=new Properties();
				prop.load(getServletContext().getResourceAsStream("/view/resources/prop/DbConf.properties"));
				DBConf dbconf=new DBConf();
				dbconf.setDriverName(prop.getProperty("driverName"));
				dbconf.setPassword(prop.getProperty("password"));
				dbconf.setUrl(prop.getProperty("url"));
				dbconf.setUserName(prop.getProperty("userName"));
				System.out.println("Testing Properties file url value:::"+dbconf.getUrl());
				TaskServices taskServ=new TaskServices(dbconf);

				response.setContentType("text/html");
		        PrintWriter out=response.getWriter();
		        System.out.println("Outside The While Loop");
		        //System.out.println(req.getParameter("region"));

		        System.out.println("domainName :"+domainName);
		        ResultSet rs = null;
		        try
		        {
		            rs=taskServ.getSubCategory(categoryName);

		        	  /* out.write("<option>");
	                   out.write("--Select--"+"</option>");*/
		           while(rs.next())
		                {
		                   // System.out.print("Within While Loop");
		                    out.write("<option value=\""+rs.getString(1)+"\">");
		                    out.write(rs.getString(1)+"</option>");
		                }
		            }


		        catch (Exception e)
		        {
		            System.out.println("Exception From Servlet"+e);
		            out.print("<option></option>");
		        }
		        finally {
					try {
						rs.close();
					} catch (SQLException sqe) {
						System.out.println("Exception caught in getSubDomainName :"+sqe);
					}
				}
		        return;
		}


		//AutoComplete for AssignTo field
		if(nm!=null && !nm.equals("")){
			System.out.println("AutoComplete for nm: "+nm);
			Properties prop=new Properties();
			prop.load(getServletContext().getResourceAsStream("/view/resources/prop/DbConf.properties"));
			DBConf dbconf=new DBConf();
			dbconf.setDriverName(prop.getProperty("driverName"));
			dbconf.setPassword(prop.getProperty("password"));
			dbconf.setUrl(prop.getProperty("url"));
			dbconf.setUserName(prop.getProperty("userName"));
			System.out.println("Testing Properties file url value:::"+dbconf.getUrl());

			TaskServices taskServ=new TaskServices(dbconf);
			response.setContentType("text/html");
	        PrintWriter out=response.getWriter();
	        ResultSet rs=taskServ.getUserId(nm.trim());
	        	try
		        {
        			while(rs.next())
	                {
	                    out.println(rs.getString(1));
	                }

		        }
		        catch (Exception e)
		        {
		            System.out.println("Exception From Servlet"+e);
		         }
		        finally {
					try {
						rs.close();
					} catch (SQLException sqe) {
						System.out.println("Exception caught in getSubDomainName :"+sqe);
					}
				}
	      	return;
		}

}


	/**
	 * Do post.
	 *
	 * @param request the request
	 * @param response the response
	 * @throws ServletException the servlet exception
	 * @throws IOException Signals that an I/O exception has occurred.
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
